﻿using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using WebSite.ViewModels.IndexViewModels;

namespace WebSite.ViewModels.Fields
{
    public class CheckBoxField : FieldBase
    {
        private readonly bool m_IsChecked;
        private readonly bool m_IsEnabled;

        public CheckBoxField(string caption, string name, bool isChecked, bool isEnabled = true) 
            : base(caption, name, isChecked)
        {
            m_IsChecked = isChecked;
            m_IsEnabled = isEnabled;
        }

        public override IHtmlString Render(HtmlHelper htmlHelper)
        {
            if (m_IsEnabled)
            {
                return htmlHelper.CheckBox(Name, m_IsChecked);
            }
            return MvcHtmlString.Create(
                htmlHelper.CheckBox(IndexViewModel.HiddenKey + Name, m_IsChecked, new {disabled = "disabled"}).ToHtmlString() +
                htmlHelper.Hidden(Name, "true").ToHtmlString());
        }
    }
}